<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<!-- head 头部 -->
<head th:replace="admin/layout/include::head('产品管理','')"></head>
<style>
    /* 表单标题加虚线 */
    .form-item-title {
        text-align: center;
        position: relative;
    }

    .form-item-title:before {
        content: "";
        position: absolute;
        border-top: 1px dashed #ccc;
        left: 40px;
        right: 40px;
        top: 8px;
        z-index: -1;
    }

    .form-item-title > span {
        background-color: white;
        padding: 0 10px;
        font-size: 13px;
        color: #666;
    }
</style>
<body>
<!-- loading 动画-->
<div th:replace="admin/layout/include::loading"></div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="pull-left layui-form-pane search-box">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <!--<button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>-->
                    </div>
                </div>
            </div>

            <!-- 数据表格 -->
            <table class="layui-table" id="data-table" lay-filter="data-table"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="data-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" ajax-event="del" ajax-url="/admin/goods/" value="{{d.goodsId}}">删除</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="data-table-state">
    <input type="checkbox" lay-filter="updateState" ajax-url="/admin/goods/updateState" value="{{d.goodsId}}" lay-skin="switch" lay-text="正常|禁用"
           {{d.state==0?'checked':''}}/>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="data-table-hot">
    <input type="checkbox" lay-filter="updateState" ajax-url="/admin/goods/updateHot" ajax-state="hot" value="{{d.goodsId}}" lay-skin="switch" lay-text="热销|一般"
           {{d.hot==1?'checked':''}}/>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="data-table-userGroup">
    <!--类型-->
    {{#  if(d.userGroup == 1){ }}
    <span style="color: #009688;">普通VIP</span>
    {{#  } else if(d.userGroup == 2){ }}
    <span style="color: #5FB878;">超级VIP</span>
    {{#  } else if(d.userGroup == 3){ }}
    <span style="color: #1E9FFF;">至尊VIP</span>
    {{#  } else{ }}

    {{#  }}}
</script>
<script type="text/html" id="toolbarOepration">
<div class="layui-btn-group">
    <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add"><i
            class="layui-icon">&#xe654;</i>添加</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" ajax-event="batchDel" ajax-url="/admin/goods/" ajax-id="goodsId">
        <i class="layui-icon layui-icon-delete"></i>批量删除</button>
</div>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelGoods">
    <form id="modelGoodsForm" lay-filter="modelGoodsForm" class="layui-form layui-row model-form">
        <input name="goodsId" type="hidden"/>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label required">产品名称</label>
                    <div class="layui-input-block">
                        <input name="goodsName" placeholder="请输入产品名称" type="text" class="layui-input" maxlength="20"
                               lay-verType="tips" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">价格</label>
                    <div class="layui-input-block">
                        <input name="money" placeholder="请输入价钱，带小数点后面" type="number" class="layui-input" maxlength="20"
                               lay-verType="tips" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">原来价格</label>
                    <div class="layui-input-block">
                        <input name="primaryMoney" placeholder="请输入原来价格" type="number" class="layui-input" maxlength="20"
                               />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="type" lay-filter="type" value="1" title="资源类型" checked/>
                        <input type="radio" name="type" lay-filter="type" value="2" title="VIP用户组"/>
                    </div>
                </div>
                <div class="layui-form-item gold">
                    <label class="layui-form-label">金币</label>
                    <div class="layui-input-block">
                        <input name="gold" placeholder="金币（整数）" type="number" class="layui-input" maxlength="20"/>
                    </div>
                </div>
                <div class="layui-form-item userGroup">
                    <label class="layui-form-label">VIP角色</label>
                    <div class="layui-input-block">
                        <select name="userGroup">
                            <option value="">请选择类型</option>
                            <option value="1">普通用户</option>
                            <option value="2">超级VIP</option>
                            <option value="3">至尊VIP</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item form-item-title"><span>产品配置</span></div>
                <div class="layui-form-item">
                    <label class="layui-form-label">产品亮点</label>
                    <div class="layui-input-block">
                        <textarea name="point" placeholder="支持HTML，用li" class="layui-textarea" maxlength="200"/>
                    </div>
                </div>
                <div class="layui-form-item form-item-title"><span>产品描述</span></div>
                <div class="layui-form-item">
                    <label class="layui-form-label">产品描述</label>
                    <div class="layui-input-block">
                        <textarea name="goodsDesc" placeholder="请输入描述" class="layui-textarea" maxlength="200"/>
                    </div>
                </div>


            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="submit" lay-submit>保存</button>
        </div>
    </form>
</script>
</body>
<!--类型-->
<!-- js部分 -->
<div th:replace="admin/layout/include::js"></div>
<script>
    layui.use(['form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var layer = layui.layer;
        var formUrl;  // 表单提交的action

        //渲染表格
        var tableIns = table.render({
            elem: '#data-table',
            url: '/admin/goods/',
            page : true,
            toolbar: '#toolbarOepration',
			loading : true,
			height : 'full-115',
			limit : 10,
			limits : [ 10, 20, 50, 100 ],
            response:{
	        	countName: 'total'
	        },
            cols: [
                [
                {type:'checkbox'},
                {field: 'goodsId', title: '#',width:40},
                {field: 'goodsName', title: '产品名称',width:130},
                {field: 'money',title:'产品价格',width:100,sort:true},
                {field: 'primaryMoney',title:'原先价格',width:100,sort:true},
                {field: 'type', title: '产品类型',templet: function (d) {
                        return d.type==1?'资源类型':d.type==2?'VIP用户组':'其他类型';
                }},
                {field: 'gold', title: '金币',width:80},
                {field: 'userGroup', title: 'VIP组',width:100,templet:'#data-table-userGroup'},
                {field: 'hot', title: '热销',width:120,templet: '#data-table-hot'},
                {field: 'state', title: '状态',templet: '#data-table-state'},
                {
                    field: 'createTime',width:160, sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center',width:160, toolbar: '#data-table-bar', title: '操作'}
            ]
            ]
        });

        form.on('radio(type)', function(data){
            if(data.value==1){
                $('.gold').show();
                $('.userGroup').hide();
            }else{
                $('.gold').hide();
                $('.userGroup').show();
            }
        });
        //头工具栏事件
        table.on('toolbar(data-table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    showEditModel();
                    break;
            };
        });


        // 工具条点击事件
        table.on('tool(data-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            }
        });

        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var keyword = $('#edtSearch').val();
            tableIns.reload( {where: {keyword: keyword}},'data');
        });

        // 显示表单弹窗
        function showEditModel(mData) {
            admin.open({
                type: 1,
                area: '745px',
                title: (mData ? '修改' : '添加') + '产品',
                content: $('#modelGoods').html(),
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    $('.userGroup').hide();
                    var url = '/admin/goods/';
                    // 回显数据
                    form.val('modelGoodsForm', mData);
                    if(mData && mData.type=='2'){
                        $('.gold').hide();
                        $('.userGroup').show();
                    }
                    // 表单提交事件
                    form.on('submit(submit)', function (data) {
                        layer.load(2);
                        // 表单提交事件
                        B.post({
                            url: '/admin/goods/',
                            loading: true,
                            data: data.field,
                            success: function (res) {
                                layer.closeAll('loading');
                                if (res.success) {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1});
                                    tableIns.reload({}, 'data');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                        return false;
                    });
                }
            });
        }
        // 修改状态
        form.on('switch(data-table-state)', function (obj) {
            B.post({
                url: '/admin/goods/updateState',
                loading: true,
                data: {goodsId: obj.elem.value, state: obj.elem.checked ? 0 : 1},
                success: function (data) {
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                }

            });

        });
        // 修改状态
        form.on('switch(data-table-hot)', function (obj) {
            B.post({
                url: '/admin/goods/updateHot',
                loading: true,
                data: {goodsId: obj.elem.value, hot: obj.elem.checked ? 1 : 0},
                success: function (data) {
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                }

            });

        });

    });

</script>
</html>